<template>
  <div class="app-container">
    <el-card v-show="showSearch" class="mb10" shadow="hover">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="100px" size="small">
        <el-form-item label="油站" prop="params.stationId">
          <el-select v-model="queryParams.params.stationId" clearable placeholder="油站">
            <el-option
              v-for="item in stationList"
              :key="item.id"
              :label="item.fullName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="油枪号" prop="params.gunNo">
          <el-input
            v-model="queryParams.params.gunNo"
            clearable
            placeholder="请输入油枪号"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="油品ID" prop="params.fuelId">
          <el-select v-model="queryParams.params.fuelId" clearable placeholder="请选择油品">
            <el-option
              v-for="item in fuelList"
              :key="item.id"
              :label="item.fuelName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="ml10">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="hover">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:gun:add']"
            icon="el-icon-plus"
            plain
            size="mini"
            type="primary"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:gun:edit']"
            :disabled="single"
            icon="el-icon-edit"
            plain
            size="mini"
            type="success"
            @click="handleUpdate"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:gun:remove']"
            :disabled="multiple"
            icon="el-icon-delete"
            plain
            size="mini"
            type="danger"
            @click="handleDelete"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:gun:export']"
            icon="el-icon-download"
            plain
            size="mini"
            type="warning"
            @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="gunList" border @sort-change="handleSortChange"
                @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column align="center" label="油枪编号" prop="gunNo" sortable sortable/>
        <el-table-column align="center" label="油站名称" prop="stationName"/>
        <el-table-column align="center" label="油罐" prop="tankName" show-overflow-tooltip/>
        <el-table-column align="center" label="油品" prop="fuelName"/>
        <el-table-column align="center" label="创建人名称" prop="creatorName"/>
        <el-table-column align="center" label="创建时间" prop="createDate" show-overflow-tooltip sortable sortable>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="编辑时间" prop="modifiedDate" show-overflow-tooltip sortable>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.modifiedDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注信息" prop="remark" show-overflow-tooltip/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                v-hasPermi="['base:gun:edit']"
                icon="el-icon-edit-outline"
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"/>
            </el-tooltip>

            <el-tooltip content="删除" placement="top">
              <el-button
                v-hasPermi="['base:gun:remove']"
                icon="el-icon-delete"
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"/>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改油枪基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="580px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="油枪号" prop="gunNo">
          <el-input v-model="form.gunNo" placeholder="请输入油枪号"/>
        </el-form-item>
        <el-form-item label="油站" prop="stationId">
          <el-select v-model="form.stationId" clearable placeholder="请选择油站">
            <el-option
              v-for="item in stationList"
              :key="item.id"
              :label="item.fullName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="油罐名称" prop="tankId">
          <el-select v-model="form.tankId" placeholder="请选择油罐" @change="changeFuel">
            <el-option
              v-for="item in tankData"
              :key="item.id"
              :label="item.tankName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="油品名称" prop="fuelId">
          <el-input
            v-model="fuelName"
            placeholder="油品名称"
            readonly="false">
          </el-input>
        </el-form-item>
        <el-form-item label="误差值" prop="difference">
          <el-input v-model="form.difference" placeholder="请输入误差值"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" :rows="2" placeholder="请输入备注信息" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addGun, getGun, listGun, updateGun} from "@/api/base/gun";
import {formatDateTime, nowCnFormatDateTime} from "@/utils/date";
import {selectList as selectFuelList} from "@/api/base/fuel";
import {selectList as selectStationList} from "@/api/base/station";
import {selectList as selectTankList} from "@/api/base/tank";

export default {
  name: "Gun",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 油枪基础信息表格数据
      gunList: [],
      // 油站数据
      stationList: [],
      // 油品数据
      fuelList: [],
      // 油管数据
      tankList: [],
      // 弹出层标题
      title: "",
      // 油品名称
      fuelName: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {
          gunNo: undefined,
          mchId: undefined,
          stationId: undefined,
          tankId: undefined,
          fuelId: undefined,
          latestValue: undefined,
          difference: undefined
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gunNo: [
          {
            required: true, message: "油枪号不能为空", trigger: "blur"
          }
        ],
        stationId: [
          {
            required: true, message: "油站ID不能为空", trigger: "blur"
          }
        ],
        tankId: [
          {
            required: true, message: "油罐ID不能为空", trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
    selectStationList().then(res => {
      this.stationList = res.data;
    });
    selectFuelList().then(res => {
      this.fuelList = res.data;
    });
    selectTankList().then(res => {
      this.tankList = res.data;
    });
  },
  computed: {
    tankData() {
      if (this.tankList != null && this.form.stationId != null) {
        return this.tankList.filter(res => {
          return res.stationId === this.form.stationId;
        })
      }
      return this.tankList;
    }
  },
  methods: {
    formatDateTime,
    /** 查询油枪基础信息列表 */
    getList() {
      this.loading = true;
      listGun(this.queryParams).then(response => {
        this.gunList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    handleSortChange(data) {
      console.log('handleSortChange')
      console.log(data)
      if (data.order) {
        if (data.order.indexOf('asc') > -1) {
          this.queryParams.sortOrder = 'asc'
        } else {
          this.queryParams.sortOrder = 'desc'
        }
        this.queryParams.sortName = data.prop
      }
      console.log(this.queryParams)
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gunNo: undefined,
        mchId: undefined,
        stationId: undefined,
        tankId: undefined,
        fuelId: undefined,
        latestValue: undefined,
        difference: undefined,
        creatorId: undefined,
        creatorName: undefined,
        createDate: undefined,
        modifierId: undefined,
        modifierName: undefined,
        modifiedDate: undefined,
        active: undefined,
        remark: undefined,
        version: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加油枪基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getGun(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.changeFuel(this.form.tankId);
        this.open = true;
        this.title = "修改油枪基础信息";
      });
    },
    /** 提交按钮 */
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGun(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGun(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除油枪基础信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delGun(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/gun/export', {
        ...this.queryParams
      }, `油枪基础信息_${nowCnFormatDateTime()}.xlsx`)
    },
    changeFuel(value) {
      // 先找出油枪ID
      let tankData = this.tankList.filter(item => {
        return item.id === value;
      })
      this.form.fuelId = tankData[0].fuelId;
      let fuelData = this.fuelList.filter(item => {
        return item.id === this.form.fuelId;
      })
      this.fuelName = fuelData[0].fuelName;
    }
  }
};
</script>

<style>
.el-form .el-select {
  width: 100%;
}
</style>
